package com.starhub.common.security.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class SystemStartupListener implements ApplicationListener<ApplicationStartedEvent> {

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        try {
            Environment env = event.getApplicationContext().getEnvironment();
            String ip = InetAddress.getLocalHost().getHostAddress();
            String port = env.getProperty("server.port");
            String contextPath = env.getProperty("server.servlet.context-path", "");
            String activeProfile = String.join(",", env.getActiveProfiles());
            
            log.info("====================================================");
            log.info("系统启动成功！");
            log.info("启动时间: {}", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            log.info("系统环境: {}", activeProfile.isEmpty() ? "default" : activeProfile);
            log.info("访问地址: http://{}:{}{}", ip, port, contextPath);
            log.info("====================================================");
        } catch (UnknownHostException e) {
            log.error("获取系统IP地址失败", e);
        }
    }
} 